﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using System.Collections;
using System.Data;
using DataRecv.db;

namespace DataRecv.models
{
    public class KKTongJiParam
    {
        /// <summary>
        /// 表名称
        /// </summary>
        private const string c_TableName = "KKTongJiByHour_t";
        /// <summary>
        /// 字段集合
        /// </summary>
        private const string c_Columns = "ip,shijian,allcount,h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15,h16,h17,h18,h19,h20,h21,h22,h23";
        /// <summary>
        /// 字段数量
        /// </summary>
        private const int c_ColumnsCount = 27;

        public string ip;
        public string roadname;
        public string fx;
        public string shijian;
        public int[] h;

        public KKTongJiParam(string deviceip,string lukou,string fangxiang)
        {
            ip = deviceip;
            roadname = lukou;
            fx = fangxiang;
            shijian = DateTime.Now.ToString("yyyy-MM-dd");
            h = new int[25];
            for (int i = 0; i < 25; i++)
            {
                h[i] = 0;
            }
            LoadCount(ip, shijian);

        }

        private void LoadCount(string ip,string shijjian)
        {
            DataTable dt = null;
            try
            {
                dt = DBHelper.GetInstance().Select(c_TableName, c_Columns, "ip='" + ip + "' and shijian='" + shijian + "'");
                if ((dt != null) && (dt.Rows != null) && (dt.Rows.Count == 1))
                {
                    h[0] = int.Parse(dt.Rows[0]["allcount"].ToString());
                    h[1] = int.Parse(dt.Rows[0]["h0"].ToString());
                    h[2] = int.Parse(dt.Rows[0]["h1"].ToString());
                    h[3] = int.Parse(dt.Rows[0]["h2"].ToString());
                    h[4] = int.Parse(dt.Rows[0]["h3"].ToString());
                    h[5] = int.Parse(dt.Rows[0]["h4"].ToString());
                    h[6] = int.Parse(dt.Rows[0]["h5"].ToString());
                    h[7] = int.Parse(dt.Rows[0]["h6"].ToString());
                    h[8] = int.Parse(dt.Rows[0]["h7"].ToString());
                    h[9] = int.Parse(dt.Rows[0]["h8"].ToString());
                    h[10] = int.Parse(dt.Rows[0]["h9"].ToString());
                    h[11] = int.Parse(dt.Rows[0]["h10"].ToString());
                    h[12] = int.Parse(dt.Rows[0]["h11"].ToString());
                    h[13] = int.Parse(dt.Rows[0]["h12"].ToString());
                    h[14] = int.Parse(dt.Rows[0]["h13"].ToString());
                    h[15] = int.Parse(dt.Rows[0]["h14"].ToString());
                    h[16] = int.Parse(dt.Rows[0]["h15"].ToString());
                    h[17] = int.Parse(dt.Rows[0]["h16"].ToString());
                    h[18] = int.Parse(dt.Rows[0]["h17"].ToString());
                    h[19] = int.Parse(dt.Rows[0]["h18"].ToString());
                    h[20] = int.Parse(dt.Rows[0]["h19"].ToString());
                    h[21] = int.Parse(dt.Rows[0]["h20"].ToString());
                    h[22] = int.Parse(dt.Rows[0]["h21"].ToString());
                    h[23] = int.Parse(dt.Rows[0]["h22"].ToString());
                    h[24] = int.Parse(dt.Rows[0]["h23"].ToString());
                }
            }
            catch { }
            finally
            {
                if (dt != null) dt.Dispose();
            }
        }

        public bool Save()
        {
            bool ans = false;
            try
            {
                string[] paramnames = c_Columns.Split(',');
                string[] paramvalues = new string[c_ColumnsCount];
                paramvalues[0] = ip;
                paramvalues[1] = shijian;
                paramvalues[2] = h[0].ToString();
                paramvalues[3] = h[1].ToString();
                paramvalues[4] = h[2].ToString();
                paramvalues[5] = h[3].ToString();
                paramvalues[6] = h[4].ToString();
                paramvalues[7] = h[5].ToString();
                paramvalues[8] = h[6].ToString();
                paramvalues[9] = h[7].ToString();
                paramvalues[10] = h[8].ToString();
                paramvalues[11] = h[9].ToString();
                paramvalues[12] = h[10].ToString();
                paramvalues[13] = h[11].ToString();
                paramvalues[14] = h[12].ToString();
                paramvalues[15] = h[13].ToString();
                paramvalues[16] = h[14].ToString();
                paramvalues[17] = h[15].ToString();
                paramvalues[18] = h[16].ToString();
                paramvalues[19] = h[17].ToString();
                paramvalues[20] = h[18].ToString();
                paramvalues[21] = h[19].ToString();
                paramvalues[22] = h[20].ToString();
                paramvalues[23] = h[21].ToString();
                paramvalues[24] = h[22].ToString();
                paramvalues[25] = h[23].ToString();
                paramvalues[26] = h[24].ToString();
                ans = DBHelper.GetInstance().EditorAddParam(c_TableName, paramnames, paramvalues);
            }
            catch { ans = false; }
            return ans;
        }
    }
}
